package com.njwb.day11.reflection2;

import java.lang.reflect.Field;

public class BeanUtil {
    public static <T> T copy(T obj) throws Exception {
        Class<?> cls = obj.getClass();
        T newObj = (T) cls.getDeclaredConstructor().newInstance();

        for (Field field : cls.getDeclaredFields()) {
            field.setAccessible(true);
            Object value = field.get(obj);
            field.set(newObj, value);
        }

        return newObj;
    }
}
